/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.gp.patient;

import be.ugent.medirem.logic.GpRemote;
import be.ugent.medirem.persistence.entity.details.ConsultationDetails;
import be.ugent.medirem.persistence.entity.details.GpDetails;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class PatientServlet extends HttpServlet {

    @EJB
    private GpRemote gpBean;

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getUserPrincipal().getName();
        Long id = gpBean.getGpId(username);
        request.getSession().removeAttribute("pid");
        List<PatientDetails> list = null;
        try {
            list = gpBean.getPatients(id);
            GpDetails details = gpBean.getDetails(id);
            if (details != null) {
                request.setAttribute("gpName", details.getName());
            }
        } catch (EntityNotFoundException ex) {
            Logger.getLogger(PatientServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(PatientServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (list != null) {
            for (PatientDetails patient : list) {
                ConsultationDetails c = gpBean.getNextConsultation(id, patient.getPatientId());
                if (c != null) {
                    patient.setNextConsultation(c.getDate());
                }
                c = gpBean.getLastConsultation(id, patient.getPatientId());
                if (c != null) {
                    patient.setLastConsultation(c.getDate());
                }
            }
            List<ConsultationDetails> c = gpBean.getConsultations(id);
            request.setAttribute("consultations", c);
            Collections.sort(list, new Comparator<PatientDetails>() {

                public int compare(PatientDetails o1, PatientDetails o2) {
                    return (o1.getSurname() + o1.getFirstname()).compareToIgnoreCase(o2.getSurname() + o2.getFirstname());
                }
            });
        }

        request.setAttribute("patients", list);
        request.getRequestDispatcher("/gp/patient/list.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
